import 'dart:math';

import 'package:eplorex/pages/community/moment/momentinfo_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

import '../../../dao/home_feed_dao.dart';
import '../../../models/HomeItemModel.dart';
import '../../community/water_card_widget.dart';

class MyMomentModule extends StatefulWidget {
  const MyMomentModule({super.key});

  @override
  State<MyMomentModule> createState() => _MyMomentModuleState();
}

class _MyMomentModuleState extends State<MyMomentModule> {
  Future<List<HomeItemModel>>? _fetchData;
  List _waterFallList = [];

  @override
  void initState() {
    super.initState();
    _fetchData = HomeDao.fetchMy();
    var randomNum = Random();
    for (var i = 0; i < 40; i++) {
      _waterFallList.add(240 + 150 * randomNum.nextDouble());
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: FutureBuilder(
        future: _fetchData,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting ) {
            return const Center(
              child: Text("loading....."),
            );
          }

          if (snapshot.connectionState == ConnectionState.done &&
              snapshot?.data==null) {
            return Center(
              //  margin: EdgeInsets.only(top: 80),
              // height: 200,
              child: Column(
                children: [Text("空空如也，快快打卡吧～～")],
              ),
            );
          }

          return MasonryGridView.count(
            // 展示几列
            crossAxisCount: 2,
            // 元素总个数
            itemCount: snapshot?.data.length,
            // 单个子元素
            itemBuilder: (BuildContext context, int index) =>WaterCardWidget(item: snapshot.data[index], height: _waterFallList[index],),
            // 纵向元素间距
            mainAxisSpacing: 10,
            // 横向元素间距
            crossAxisSpacing: 10,
            //本身不滚动，让外面的singlescrollview来滚动
            // physics: const NeverScrollableScrollPhysics(),
            shrinkWrap: true, //收缩，让元素宽度自适应
          );
        },
      ),
    );
  }

}
